package com.taotao.sso.api.impl;

import java.io.IOException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.taotao.common.service.RedisService;
import com.taotao.sso.bean.User;
import com.taotao.sso.service.IUserService;

@Service
public class UserService implements IUserService{
	
	@Autowired
	private RedisService redisService;
	
	private static final ObjectMapper MAPPER= new ObjectMapper();
	
	public User queryUserByToken(String token) {
		String key = "TOKEN_" + token;
		String userJson = this.redisService.get(key);
		User user = null;
		try {
			if (null == userJson) {
				// 登录超时
				return null;
			}
			user = MAPPER.readValue(userJson, User.class);
	        // 重新设置redis中的时间,非常重要
	        this.redisService.expire(key, 1800);
		} catch (JsonParseException e) {
			e.printStackTrace();
		} catch (JsonMappingException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return user;
	}
}
